<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function _initaialize(){
        if (!session('?admin')) {
            $this->error('权限不足','/Long/login/');
            $this->redirect('Login/login');
            exit;
        }
    }
    public function index(){
        echo "this is Admin";
    }

   /* function userlist(){
    	$userModel=M('users');
    	$local_users=$userModel->order('id desc')->select();
    	//select * from think_users order by id desc
    	$this->users=$local_users;

    	$this->dispaly();
    	//吧当前对象的所有全局变量传递到对应的视图中，可以使用assign方法限制传递的全局变量。
    }*/
        public function userlist(){
        $usersmodel=M('users');
        $users=$usersmodel->order('id desc')->select();
        $this->users=$users;
        $this->display();
    }


    public function bsUserList(){
		/*$usersmodel=M('users');
		$users=$usersmodel->order('id desc')->select();
		$this->users=$users;
		$this->display();*/


    $User = M('Users'); // 实例化User对象
    $count = $User->count();// 查询满足要求的总记录数
    $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    $show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    $list = $User->limit($Page->firstRow.','.$Page->listRows)->select();
//  $this->users=$list;
//  $this->page=$show
    $this->assign('users',$list);// 赋值数据集
    $this->assign('page',$show);// 赋值分页输出
    $this->display(); // 输出模板
	}


   public function users(){
        $usersmodel=M('users');
        $users=$usersmodel->order('id desc')->select();
        $this->users=$users;
        $this->display();
    }
    function del($id){
        $usersmodel=M('Users');
        //delete from think_users where id=$id
        if ($usersmodel->delete($id)) {//删除成功
           $this->success('删除成功');
        }
        else{
            $this->error('删除失败');

        }

    }
    function setstar($id){
        $usersmodel=M('Users');
        $date['isstar']=1;
        if ($usersmodel->where("id=$id")->save($date)) {//update users set isstar=1 where id=$id
            $this->success('修改成功！');
        }
        else{
        $this->error('修改失败！');
        }
    }
    function unstar($id){
        $usersmodel=M('Users');
        $usersmodel->isstar=0;
        $usersmodel->isfurst=0;
        $usersmodel->id=$id;
        if ($usersmodel->save() ) {
            $this->success('取消明星作者成功');
        }
        else{
            $this->error('取消明星作者失败');
        }
    }
    function setisfirst($id){
        $usersmodel=M('Users');
        $usersmodel->isfirst=1;
        $usersmodel->isstar=1;
        $usersmodel->id=$id;
        if ($usersmodel->save() ) {
            $this->success('设置首页推荐成功');
        }
        else{
            $this->error('设置首页推荐失败');
        }
    }
    function unisfirst($id){
        $usersmodel=M('Users');
  
        $usersmodel->isfurst=0;
        $usersmodel->id=$id;
        if ($usersmodel->save() ) {
            $this->success('取消首页推荐成功');
        }
        else{
            $this->error('取消首页推荐失败');
        }
    }
    function updateUserInfo($id){
        $usersmodel=M('Users');
        $this->user=$usersmodel->find($id);
        $this->display();
    }


    function update(){
        $usersmodel=M('Users');
        $usersmodel->create();
        if ($_FILE['photo']['error']==0) {
            $fileInfo=img();
            $usersmodel->photo=$fileName;
        }
        if($usersmodel->save()){
            $this->success('修改用户成功','Users');
        }else{
            $this->error('修改用户失败');
        }
    }

    public function upload(){
        $upload= new \Think\Upload();
        $upload->maxSize = 3145728;
        $upload->exts =array('jpg','gif','png','jpeg');
        $upload->rootPath ='./Public/Uploads/photo/';
        $upload->autoSub=false;
        $info=$upload->upload();
        if(!$info){
            $this->error($upload->getError());
        }
        else{
            return $info;
        }
    }
    /*function img(){
       // if ($_FILE['photo']['error']==0) 
            $photoinfo=$this->upload();
            $filePath=$photoinfo['photo']['rootPath'];
            $oldFileName=$photoinfo['photo']['savename'];
            $olfFileFullName=$photoinfo.'\\'.$photoinfo;
            $newFileName='thumb_'.$oldFileName;
            $newFileFullName=$filePath.'\\'.$newFileName;
            $image=new \Think\Image();
            $image->open($olfFileFullName);
            $image->thumb(100,100)->save($newFileFullName);
            return $newFileFullName;
        
    }*/





     public function addUser()
    {    $user = D("Users");
         if ($user->create())
          {

             if ($_FILE['photo']['error']==0) {
            $photoinfo=$this->upload();//将临时文件保存到指定的位置
            $user->photo=$photoinfo['photo']['savename'];
            }else{
            $user->photo = 'default.jpg';}
            if (false !== $user->add())
             {
                  $this->success('注册成功！', '');
             } 
             else 
                {         $this->error('数据错误');   }
            }
                else {            
                     $this->error($user->getError());
                     }  
    }


   
}

